// columns.ts

import DictTag, { DictValueEnum } from "@/components/DictTag";
import IconFont from "@/components/IconFont";
import { createIcon } from "@/utils/IconUtil";
import { QuestionCircleOutlined } from "@ant-design/icons";
import { ProColumns } from "@ant-design/pro-table";
import { Divider, Popconfirm, Space, Typography } from "antd";

// 定义所有列的工厂函数
export const createTableColumns = (
	statusOptions: DictValueEnum,
	handleColumnEditor: (record: API.System.ApplicationVo) => void,
	handleColumnRemove: (record: API.System.ApplicationVo) => void
): ProColumns<API.System.ApplicationVo>[] => {
	return [
		{
			title: "应用名称",
			dataIndex: "appName",
			valueType: "text"
		},
		{
			title: "应用编码",
			dataIndex: "appCode",
			valueType: "text"
		},
		{
			title: "显示顺序",
			dataIndex: "sort",
			valueType: "text",
			hideInSearch: true
		},
		{
			title: "应用图标",
			dataIndex: "icon",
			valueType: "text",
			hideInSearch: true,
			render: (_, record) => {
				const icon = record?.icon;
				const iconComponent = icon ? icon.startsWith("icon-") ? <IconFont type={icon} /> : createIcon(icon) : "-";
				return <>{iconComponent} </>;
			}
		},
		{
			title: "应用状态",
			dataIndex: "status",
			valueType: "select",
			valueEnum: statusOptions,
			render: (_, record) => {
				return <DictTag type="badge" enums={statusOptions} value={record.status} />;
			}
		},
		{
			title: "操作",
			dataIndex: "option",
			valueType: "option",
			fixed: "right",
			render: (_, record: any) => {
				return (
					<>
						<Space size={1} split={<Divider type="vertical" />}>
							<Typography.Link
								key="edit"
								onClick={() => {
									handleColumnEditor(record);
								}}
							>
								编辑
							</Typography.Link>
							{record.id !== 1 && (
								<Popconfirm
									placement="bottomRight"
									title={"警告"}
									description={() => {
										return (
											<div style={{ width: 260 }}>
												您确定要删除应用，名为“{record?.appName}”吗？
												<br />
												执行此操作后，相关数据将被永久删除，无法恢复。
											</div>
										);
									}}
									onConfirm={() => {
										handleColumnRemove(record);
									}}
									icon={<QuestionCircleOutlined style={{ color: "red" }} />}
									okText="确认"
									cancelText="取消"
								>
									<Typography.Link type="danger" key="batchRemove">
										删除
									</Typography.Link>
								</Popconfirm>
							)}
						</Space>
					</>
				);
			}
		}
	];
};

export const proTableCardProps = {
	bodyStyle: {
		paddingInline: 0,
		paddingBlock: 0
	}
};

export const pagination = {
	showQuickJumper: true,
	showSizeChanger: true
};

export const formTitle = ["创建应用", "修改应用"];
